package com.stray.crawl.expr.psr.jsoup;

import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.stray.crawl.excp.ParseOperationException;
import com.stray.crawl.expr.Express;

public class HtmlParser extends Express {

	@Override
	protected Object doParse(Object obj, String args) {
		String str = null;
		if (obj instanceof Element) {
			str = ((Element) obj).html();
		} else if (obj instanceof Elements) {
			str = ((Elements) obj).html();
		} else {
			throw new ParseOperationException("Invalid input object for parser. Request: Elements / Element, Input: " + obj.getClass());
		}

		return str != null ? StringEscapeUtils.escapeHtml4(str) : obj;
	}

}